<template>
  <page-container>
    <search-form
      v-model="searchForm"
      :searchColumns="searchColumns"
      :searchOptions="{ menuSpan: 5 }"
      @on-search="handleSearch">
      <template v-slot:newBtn>
        <el-button
          type="primary"
          @click="handelAddForm()"
          >充值</el-button
        >
      </template>
    </search-form>

    <avue-table
      :dataList="dataList"
      :loading="loading"
      :page="page"
      :tableColumns="tableColumns"
      @onPageChange="onPageChange">
    </avue-table>
  </page-container>
</template>

<script>
import tableMixins from '@/mixins/table'
import { deepClone } from '@/utils'
import { mapGetters } from 'vuex'
import { queryRechargeCoin, addRechargeCoinForm } from '@/api/userManage'
import { searchColumns, tableColumns } from './config/table-config'
import { addDialogForm } from './config/dialogForm'

export default {
  mixins: [tableMixins],
  data() {
    return {
      dataList: [],
      searchForm: {
        dateRange: [this.$startStamp(new Date()), this.$endStamp(new Date())],
      },
      editForm: {},
    }
  },
  computed: {
    ...mapGetters(['commonData', 'countryList']),
    userList() {
      return this.commonData?.userList ?? []
    },
    addDialogForm,
    searchColumns,
    tableColumns,
  },
  methods: {
    fetchMethod: queryRechargeCoin,

    // 查询参数格式化
    formatSearch() {
      const searchParams = deepClone(this.searchForm)
      const [start, end] = this.searchForm.dateRange
      searchParams.start = start && this.$dayjs(this.$startStamp(start)).unix()
      searchParams.end = end && this.$dayjs(this.$endStamp(end)).unix()
      delete searchParams.dateRange
      return searchParams
    },
    handelAddForm() {
      this.$dialogForm.show({
        width: '30%',
        title: '手动充值',
        option: {
          span: 24,
          column: this.addDialogForm,
        },
        callback: async ({ data, close }) => {
          const parms = {
            ...data,
            coins: data?.coins * 10000,
          }
          const resData = await addRechargeCoinForm(parms)
          if (resData.code === 0) {
            this.$message.success('新增成功')
          }
          this.getList()
          close()
        },
      })
    },
  },
  mounted() {
    this.handleSearch()
  },
}
</script>

<style lang="scss" scoped></style>
